package org.apache.pdfbox.arktaohtml.slide;

import lombok.extern.slf4j.Slf4j;

import java.text.MessageFormat;

@Slf4j
public class AText extends AElement implements AIElement {

    // move to parents
    // private String type = String.valueOf(AElementType.TEXT).toLowerCase();       // | `'text'`，不可更改                                           | string   | 'text'
    //private HSLFShape shape; // POI 中的 shape 对象
    //private double left;                     //| 在画布中的水平坐标，单位 `px`                                | number   | 0                               |
    //private double top;                      //| 在画布中的垂直坐标，单位 `px`                                | number   | 0                               |
    //private double width;                    //| 宽度，单位 `px`                                              | number   | 300                             |
    //private double height;                   //| 高度，单位 `px`                                              | number   | 0                               |
    //private double rotate;                   //| 旋转角度，单位 `deg`                                         | number   | 0                               |
    //protected double slideWidth;                 // 幻灯片的宽
    //protected double slideHeight;                 //幻灯片的高
    //    private double opacity;                 //| 不透明度，范围0~1                                            | number   | 1                               |

    public static final String defaultColor = "#000000";
    public static final String defaultFontWeight = "normal";
    public static final String defaultFontStyle = "normal";
    public static final String defaultPosition = "absolute";
    public static final String transparentColor = "rgba(0,0,0,0)";

    private double lineHeight;               //| 行高，单位 `倍`                                              | number   | 1                               |
    private String position;

    //    private ABorderStyle borderStyle;              //| 边框样式，可选实线`'solid'`和虚线`'dashed'`                  | string   |                                 |
    private double borderWidth;              //| 边框宽度，单位 `px`                                          | number   |                                 |
    private String borderColor;              //| 边框颜色                                                     | string   |                                 |
    private String fill;                     //| 文本框填充颜色                                               | string   |                                 |
    private double segmentSpacing;            //| 段间距，单位 `px`                                            | number   | 0                               |
    private double letterSpacing;            //| 字间距，单位 `px`                                            | number   | 0                               |
    private double String;                    //| 阴影样式，数据格式：`1px 1px 3px #666`具体见CSS属性 `text-shadow` | string   |                                 |
    private double[] padding = {5, 5, 5, 5};                   //| 文本框边距，单位 `px`                                        | array    | [5, 5, 5, 5]                    |
    private String[] link;                      //| 超链接，可能是：链接到网页 `['web', '网页地址']` 或 链接到课件中的某一页 `['slide', '页面id']` | array    |                                 |
    private ATextType textType;                  //| 文本类型，当前只支持正文 `'content'`                         | string   | 'content'                       |
    private String content;                   //| 富文本内容 |string   |'<div>“单击此处添加文本”</div>'|
    private Double fontSize;                    // 文字大小
    private String fontColor;                   //文字颜色
    private String text;                         // 文本内容
    private String fontWeight;
    private String fontStyle;
    private Double wordSpacing;
    private String fontFamily;
    private String strokeColor;

    public static java.lang.String getDefaultColor() {
        return defaultColor;
    }

    public static java.lang.String getDefaultFontWeight() {
        return defaultFontWeight;
    }

    public static java.lang.String getDefaultFontStyle() {
        return defaultFontStyle;
    }

    public static java.lang.String getDefaultPosition() {
        return defaultPosition;
    }

    public static java.lang.String getTransparentColor() {
        return transparentColor;
    }

    public void setLineHeight(float lineHeight) {
        this.lineHeight = lineHeight;
    }

    public java.lang.String getPosition() {
        return position;
    }

    public void setPosition(java.lang.String position) {
        this.position = position;
    }

    public java.lang.String getStrokeColor() {
        return strokeColor;
    }

    public void setStrokeColor(java.lang.String strokeColor) {
        this.strokeColor = strokeColor;
    }


    public AText() {
        super();
    }

    public AText(APowerPoint ppt) {

        setTextType(ATextType.content);
    }

    public java.lang.String getFontWeight() {
        return fontWeight;
    }

    public void setFontWeight(java.lang.String fontWeight) {
        this.fontWeight = fontWeight;
    }

    public java.lang.String getFontStyle() {
        return fontStyle;
    }

    public void setFontStyle(java.lang.String fontStyle) {
        this.fontStyle = fontStyle;
    }

    public Double getWordSpacing() {
        return wordSpacing;
    }

    public void setWordSpacing(Double wordSpacing) {
        this.wordSpacing = wordSpacing;
    }

    public void setLetterSpacing(Double letterSpacing) {
        this.letterSpacing = letterSpacing;
    }


    public double getBorderWidth() {
        return borderWidth;
    }

    public void setBorderWidth(double borderWidth) {
        this.borderWidth = borderWidth;
    }

    public java.lang.String getBorderColor() {
        return borderColor;
    }

    public void setBorderColor(java.lang.String borderColor) {
        this.borderColor = borderColor;
    }

    public java.lang.String getFill() {
        return fill;
    }

    public void setFill(java.lang.String fill) {
        this.fill = fill;
    }

    public double getOpacity() {
        return opacity;
    }

    public void setOpacity(double opacity) {
        this.opacity = opacity;
    }

    public double getLineHeight() {
        return lineHeight;
    }

    public void setLineHeight(double lineHeight) {
        this.lineHeight = lineHeight;
    }

    public double getSegmentSpacing() {
        return segmentSpacing;
    }

    public void setSegmentSpacing(double segmentSpacing) {
        this.segmentSpacing = segmentSpacing;
    }

    public double getLetterSpacing() {
        return letterSpacing;
    }

    public void setLetterSpacing(double letterSpacing) {
        this.letterSpacing = letterSpacing;
    }

    public double getString() {
        return String;
    }

    public void setString(double string) {
        String = string;
    }

    public java.lang.String[] getLink() {
        return link;
    }

    public void setLink(java.lang.String[] link) {
        this.link = link;
    }

    public java.lang.String getFontFamily() {
        return fontFamily;
    }

    public void setFontFamily(java.lang.String fontFamily) {
        this.fontFamily = fontFamily;
    }

    @Override
    public void setElementType() {
        this.type = AElementType.text;
    }

    public java.lang.String getContent() {
        return this.renderContent();
    }

    protected void setContent(java.lang.String content) {
        this.content = content;
    }

    public java.lang.String getText() {
        return text;
    }

    public void setText(java.lang.String text) {
        this.text = text;
    }

    public ATextType getTextType() {
        return textType;
    }

    public void setTextType(ATextType textType) {
        this.textType = textType;
    }



    public Double getFontSize() {
        return fontSize;
    }

    public void setFontSize(Double fontSize) {
        this.fontSize = fontSize;
    }

    public void setFontColor(String fontColor) {
        this.fontColor = fontColor;
    }

    public String getFontColor() {
        return fontColor;
    }











    /**
     * to render the text to richtext
     */
    private String renderContent() {

//        String template = "<div align=\"left\"><font size=\"{0}\" face=\"Microsoft Yahei\" color=\"{1}\">{2}</font></div>";
//        String value=MessageFormat.format(template,
//                this.getFontSize(),
//                this.getFontColor(),
//                this.getText()
//        );
        String template = "<font style=\"{0}\">{1}</font>";
        String value = MessageFormat.format(template,
                this.getStyle(),
                this.getText()
        );
        this.setContent(value);
        return value;
    }

    private void appendString(StringBuilder s, String propertyName, String value) {
        s.append(propertyName);
        s.append(':');
        s.append(value);
        s.append(';');
    }

    public String formatLength(double length) {

        //return String.format(Locale.US, "%1.1f%s", length, units); //nice but slow
        return length + units;
    }

    private void appendLength(StringBuilder s, String propertyName, Double value) {

        s.append(propertyName);
        s.append(':');
        s.append(formatLength(value));
        s.append(';');
    }

    public String getStyle() {
        StringBuilder ret = new StringBuilder();
//        todo width need to Process
//        appendLength(ret, "width", this.getWidth());
        if (position != null && !position.equals(defaultPosition))
            appendString(ret, "position", position);
        appendLength(ret, "top", this.getTop());
        appendLength(ret, "left", this.getLeft());
        appendLength(ret, "line-height", this.getLineHeight());
        if (fontFamily != null)
            appendString(ret, "font-family", this.getFontFamily());
        if (fontSize != 0) {
//            todo Fontsize need to process
            appendLength(ret, "font-size", AUtils.px2pt(this.getFontSize()));
        }


        if (fontWeight != null && !defaultFontWeight.equals(fontWeight))
            appendString(ret, "font-weight", fontWeight);
        if (fontStyle != null && !defaultFontStyle.equals(this.getFontStyle()))
            appendString(ret, "font-style", this.getFontStyle());
        if (wordSpacing != 0)
            appendLength(ret, "word-spacing", this.getWordSpacing());
        if (letterSpacing != 0)
            appendLength(ret, "letter-spacing", this.getLetterSpacing());
        if (fontColor != null && !defaultColor.equals(fontColor))
            appendString(ret, "color", getFontColor());

        if (strokeColor != null && !strokeColor.equals(transparentColor))
            log.error("Unsupported strokeColor");
//            ret.append(createTextStrokeCss(strokeColor));

        return ret.toString();
    }


    public double[] getPadding() {
        return padding;
    }

    public void setPadding(double[] padding) {
        this.padding = padding;
    }

    @Override
    public AText render() {
//        todo render
        return null;
    }

}
